import java.util.List;

public class Player {
	public static final int ERROR = -1;
	public static final int HIT = 1;
	public static final int SURRENDER = 2;
	public static final int HOLD = 3;
	public static final int DOUBLEBET = 4;
	public static final int SPLITE = 5;
	public static final int BUST = 6;

	private static boolean surrenderAble = false;

	public static boolean isSurrenderAble() {
		return surrenderAble;
	}

	public static void setSurrenderAble(boolean surrenderAble) {
		Player.surrenderAble = surrenderAble;
	}

	private static boolean isPair(List<P> myCards) {
		if (myCards.size() == 2 && myCards.get(0).getNum() == myCards.get(1).getNum()) {
			return true;
		} else {
			return false;
		}
	}

	public static int getStrategy(P dealerP, List<P> myCards) {
		int countNow = count(myCards);
		boolean hasA = hasA(myCards);
		boolean hasPair = isPair(myCards);
		if (countNow >= 22) {
			return BUST;
		}

		if (!hasA && !hasPair) {
			if (countNow <= 8) {
				return HIT;
			} else if (countNow == 9) {
				if ((dealerP.getNum() == 3 || dealerP.getNum() == 4 || dealerP.getNum() == 5 || dealerP.getNum() == 6) && myCards.size() == 2) {
					return DOUBLEBET;
				} else {
					return HIT;
				}
			} else if (countNow == 10) {
				if (dealerP.getNum() <= 9 && myCards.size() == 2) {
					return DOUBLEBET;
				} else {
					return HIT;
				}
			} else if (countNow == 11) {
				if (dealerP.getNum() <= 9 && myCards.size() == 2) {
					return DOUBLEBET;
				} else {
					return HIT;
				}
			} else if (countNow == 12) {
				if (dealerP.getNum() == 2 || dealerP.getNum() == 3) {
					return HIT;
				} else if (dealerP.getNum() == 4 || dealerP.getNum() == 5 || dealerP.getNum() == 6) {
					return HOLD;
				} else {
					return HIT;
				}
			} else if (countNow == 13) {
				if (dealerP.getNum() <= 6) {
					return HOLD;
				} else {
					return HIT;
				}
			} else if (countNow == 14) {
				if (dealerP.getNum() <= 6) {
					return HOLD;
				} else if (dealerP.getNum() == 10) {
					if (myCards.size() == 2 && surrenderAble) {
						return SURRENDER;
					} else {
						return HIT;
					}
				} else {
					return HIT;
				}
			} else if (countNow == 15) {
				if (dealerP.getNum() == 10) {
					if (myCards.size() == 2 && surrenderAble) {
						return SURRENDER;
					} else {
						return HIT;
					}
				} else if (dealerP.getNum() == 2 || dealerP.getNum() == 3 || dealerP.getNum() == 4 || dealerP.getNum() == 5 || dealerP.getNum() == 6) {
					return HOLD;
				} else {
					return HIT;
				}

			} else if (countNow == 16) {
				if (dealerP.getNum() == 10 || dealerP.getNum() == 9) {
					if (myCards.size() == 2 && surrenderAble) {
						return SURRENDER;
					} else {
						return HIT;
					}
				} else if (dealerP.getNum() == 2 || dealerP.getNum() == 3 || dealerP.getNum() == 4 || dealerP.getNum() == 5 || dealerP.getNum() == 6) {
					return HOLD;
				} else {
					return HIT;
				}

			} else if (countNow >= 17) {
				return HOLD;
			}
		}

		if (hasA && hasPair) {
			if (dealerP.getNum() == 14) {
				return HIT;
			} else {
				return SPLITE;
			}
		} else if (hasA) {
			if (dealerP.getNum() == 2) {
				return getDealer2Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 3) {
				return getDealer3Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 4) {
				return getDealer4Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 5) {
				return getDealer5Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 6) {
				return getDealer6Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 7) {
				return getDealer7Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 8) {
				return getDealer8Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 9) {
				return getDealer9Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 10 || dealerP.getNum() == 11 || dealerP.getNum() == 12 || dealerP.getNum() == 13) {
				return getDealer10Strategy(myCards, countNow, hasA);
			} else if (dealerP.getNum() == 14) {
				return getDealer11Strategy(myCards, countNow, hasA);
			}
		} else if (hasPair) {

			if (myCards.get(0).getNum() == 8) {
				if (dealerP.getNum() == 14) {
					return HIT;
				} else if (dealerP.getNum() >= 10) {
					if (myCards.size() == 2 && surrenderAble) {
						return SURRENDER;
					} else {
						return HIT;
					}
				} else {
					return SPLITE;
				}
			} else if (myCards.get(0).getNum() == 5) {
				if (dealerP.getNum() < 10) {
					return DOUBLEBET;
				} else {
					return HIT;
				}

			} else if (myCards.get(0).getNum() >= 10) {
				return HOLD;
			} else if (myCards.get(0).getNum() == 2 || myCards.get(0).getNum() == 3) {
				if (dealerP.getNum() == 2 || dealerP.getNum() == 3 || dealerP.getNum() == 4 || dealerP.getNum() == 5 || dealerP.getNum() == 6 || dealerP.getNum() == 7) {
					return SPLITE;
				} else {
					return HIT;
				}

			} else if (myCards.get(0).getNum() == 7) {
				if (dealerP.getNum() == 2 || dealerP.getNum() == 3 || dealerP.getNum() == 4 || dealerP.getNum() == 5 || dealerP.getNum() == 6 || dealerP.getNum() == 7) {
					return SPLITE;
				} else if (dealerP.getNum() == 10 || dealerP.getNum() == 11 || dealerP.getNum() == 12 || dealerP.getNum() == 13) {
					if (myCards.size() == 2 && surrenderAble) {
						return SURRENDER;
					} else {
						return HIT;
					}
				} else {
					return HIT;
				}
			} else if (myCards.get(0).getNum() == 4) {
				if (dealerP.getNum() == 5 || dealerP.getNum() == 6) {
					return SPLITE;
				} else {
					return HIT;
				}
			} else if (myCards.get(0).getNum() == 6) {
				if (dealerP.getNum() == 4 || dealerP.getNum() == 5 || dealerP.getNum() == 6 || dealerP.getNum() == 3 || dealerP.getNum() == 2) {
					return SPLITE;
				} else {
					return HIT;
				}
			} else if (myCards.get(0).getNum() == 9) {
				if (dealerP.getNum() == 7 || dealerP.getNum() >= 10) {
					return HOLD;
				} else {
					return SPLITE;
				}
			}

		}

		// else if (countNow <= 8 && !hasA && !()) {
		// return HIT;
		// } else if (countNow >= 17 && !hasA && !(myCards.size() == 2 &&
		// myCards.get(0).getNum() == myCards.get(1).getNum())) {
		// return HOLD;
		// }
		//

		return ERROR;
	}

	public static int count(List<P> pList) {
		int count = 0;
		boolean hasA = false;
		for (P p : pList) {
			if (p.getNum() == 14) {
				count = count + 1;
				hasA = true;
			} else if (p.getNum() >= 10) {
				count = count + 10;
			} else {
				count = count + p.getNum();
			}
		}

		if (hasA && count <= 11) {
			count = count + 10;
		}
		return count;
	}

	public static boolean hasA(List<P> pList) {
		boolean hasA = false;
		for (P p : pList) {
			if (p.getNum() == 14) {
				hasA = true;
			}
		}
		int count = 0;
		for (P p : pList) {
			if (p.getNum() == 14) {
				count = count + 1;
			} else if (p.getNum() >= 10) {
				count = count + 10;
			} else {
				count = count + p.getNum();
			}
		}
		if (count + 10 >= 22) {
			hasA = false;
		}

		return hasA;
	}

	private static int getDealer2Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow == 18 || countNow >= 19) {
			return HOLD;
		} else {
			return HIT;
		}

	}

	private static int getDealer3Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow == 17) {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HIT;
			}
		} else if (countNow == 18) {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HOLD;
			}
		} else if (countNow >= 19) {
			return HOLD;
		} else {
			return HIT;
		}
	}

	private static int getDealer4Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow == 13 || countNow == 14) {
			return HIT;
		} else if (countNow >= 19) {
			return HOLD;
		} else if (countNow == 18) {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HOLD;
			}
		} else {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HIT;
			}
		}
	}

	private static int getDealer5Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow >= 19) {
			return HOLD;
		} else if (countNow == 18) {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HOLD;
			}
		} else {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HIT;
			}
		}

	}

	private static int getDealer6Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow >= 19) {
			return HOLD;
		} else if (countNow == 18) {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HOLD;
			}
		} else {
			if (myCards.size() == 2) {
				return DOUBLEBET;
			} else {
				return HIT;
			}
		}

	}

	private static int getDealer7Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow >= 18) {
			return HOLD;
		} else {
			return HIT;
		}

	}

	private static int getDealer8Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow >= 18) {
			return HOLD;
		} else {
			return HIT;
		}

	}

	private static int getDealer9Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow >= 19) {
			return HOLD;
		} else {
			return HIT;
		}

	}

	private static int getDealer10Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow >= 19) {
			return HOLD;
		} else {
			return HIT;
		}

	}

	private static int getDealer11Strategy(List<P> myCards, int countNow, boolean hasA) {
		if (countNow >= 19) {
			return HOLD;
		} else {
			return HIT;
		}

	}
}
